import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/core/withdraw_deposit_manager/withdraw_deposit_request.dart';
import 'package:flutter_app_unionad/model/take_save_model.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:flutter_app_unionad/ui/basice/input_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/ui/basice/select_sex_widget.dart';
import 'package:flutter_app_unionad/ui/basice/select_title_and_detail_widget.dart';

class WithdrawDepositPage extends StatefulWidget {
  static const routeName = "/withdrawDepositPage";
  final TaskSaveModel? accountModel; //推广进来的

  const WithdrawDepositPage({Key? key, this.accountModel}) : super(key: key);

  @override
  _WithdrawDepositPageState createState() => _WithdrawDepositPageState();
}

class _WithdrawDepositPageState extends State<WithdrawDepositPage> {
  bool showHint = false;
  String? name;
  String? mobile;
  String? idCode;
  String? blankCode;
  String? money;
  int type = 1;

  //提现
  void onTap() async {
    if (name == null || name!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: '姓名不能为空');
      return;
    }

    if (mobile == null || mobile!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: '手机号不能为空');
      return;
    }

    if (!isPhoneString(mobile)) {
      ShowToastWidget.showToast(msg: '手机号格式不对');
      return;
    }

    if (idCode == null || idCode!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: '证件号不能为空');
      return;
    }

    if (blankCode == null || blankCode!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: '银行卡不能为空');
      return;
    }

    if (money == null || money!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: '提现金额不能为空');
      return;
    }

    if (num.parse(money ?? '0') < 0) {
      ShowToastWidget.showToast(msg: '提现金额不能小于0');
      return;
    }

    if (widget.accountModel != null) {
      if (num.parse(money ?? '0') < 10000) {
        ShowToastWidget.showToast(msg: '提现金币不能小于10000');
        return;
      }
      if (num.parse(money ?? '0') > widget.accountModel!.userPoint!) {
        ShowToastWidget.showToast(msg: '可提现金币不能大于${widget.accountModel!.userPoint}');
        return;
      }
    }

    FocusScope.of(context).requestFocus(FocusNode());
    ProgressDialog.showProgress(context);
    final future = await WithdrawDepositRequest.saveBankInfo(
      bankCardNumber: blankCode,
      documentNumber: idCode,
      documentType: type,
      userName: name,
      telNumber: mobile,
    );
    if (!future) {
      ProgressDialog.dismiss(context);
      return;
    }
    // return;
    print("9");

    final f1 = await WithdrawDepositRequest.withdrawDeposit(
        amount: num.parse(money ?? '0'), type: widget.accountModel != null ? 0 : 1);
    ProgressDialog.dismiss(context);
    Navigator.pop(context, f1);
  }

  @override
  Widget build(BuildContext context) {
    return BaseNavigationWidget(
      title: "填写信息",
      child: Container(
        child: buildContent(),
        padding: EdgeInsets.symmetric(vertical: 35.px, horizontal: 30.px),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.px),
        ),
      ),
      needScrollView: false,
    );
  }

  Widget buildContent() {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // buildHintWidget(),
          Text(
            "基本信息",
            style: TextStyle(
              color: MyColors.color_27_27_27,
              fontWeight: MyFontWeight.bold,
              fontSize: MyFont.font16,
            ),
          ),
          MySizeBox(height: 30),
          InputSeparatorWidget(
            valueChange: (value) => name = value,
            title: "银行预留姓名",
            placeholder: "请输入姓名",
            titleWidth: 90.px,
          ),
          MySizeBox(height: 10),
          InputSeparatorWidget(
            valueChange: (value) => mobile = value,
            title: "银行预留电话",
            placeholder: "请输入电话",
            titleWidth: 90.px,
            maxLength: 11,
            keyboard: TextInputType.number,
          ),
          MySizeBox(height: 10),
          SelectSexWidget(
            titleWidth: 70.px,
            spacing: 10.px,
            title: "证件类型",
            sex: type,
            selectSexOnTap: (value) => type == value,
            actions: [
              "身份证",
              "台湾通行证",
              "港澳台通行证",
            ],
          ),
          MySizeBox(height: 10),
          InputSeparatorWidget(
            valueChange: (value) => idCode = value,
            title: "证件号码",
            placeholder: "请输入证件号码",
            titleWidth: 90.px,
            keyboard: TextInputType.visiblePassword,
          ),
          MySizeBox(height: 10),
          InputSeparatorWidget(
            valueChange: (value) => blankCode = value,
            title: "银行卡号",
            placeholder: "请输入银行卡号",
            titleWidth: 90.px,
            keyboard: TextInputType.number,
          ),
          MySizeBox(height: 10),
          InputSeparatorWidget(
            valueChange: (value) {
              money = value;
              if (widget.accountModel != null) setState(() {});
            },
            title: widget.accountModel == null ? "提现金额" : '提现金币',
            placeholder: widget.accountModel == null ? "请输入提现金额" : "请输入提现金币",
            titleWidth: 90.px,
            keyboard: TextInputType.numberWithOptions(decimal: true),
            icon: Text(widget.accountModel == null ? '元' : ""),
          ),
          if (widget.accountModel != null) MySizeBox(height: 10),
          if (widget.accountModel != null)
            SelectTitleAndDetailWidget(
              title: "手续费",
              detail: money == null || money == ''
                  ? '  0  元'
                  : (num.parse(money ?? '0') * 0.0001 > 20
                      ? '  0  元'
                      : '${(num.parse(money ?? '0') * 0.0001 * 0.1 * 0.9).toStringAsFixed(2)}  元'),
              detailStyle: TextStyle(
                color: MyColors.color_246_45_29,
                fontWeight: MyFontWeight.regular,
                fontSize: MyFont.font14,
              ),
            ),
          if (widget.accountModel != null) MySizeBox(height: 10),
          if (widget.accountModel != null)
            SelectTitleAndDetailWidget(
              title: "等于",
              detail: money == null || money == ''
                  ? "0 元"
                  : '${(num.parse(money ?? '0') * 0.0001 * 0.9).toStringAsFixed(2)} 元',
              detailStyle: TextStyle(
                color: MyColors.color_246_45_29,
                fontWeight: MyFontWeight.regular,
                fontSize: MyFont.font14,
              ),
            ),

          if (widget.accountModel != null)
            MySizeBox(
              height: 10,
            ),
          if (widget.accountModel != null)
            Container(
              alignment: Alignment.center,
              child: Text(
                '注：单次提现低于20，收取10%手续费',
                style: TextStyle(
                  fontSize: MyFont.font12,
                  color: MyColors.color_43_44_48,
                ),
              ),
            ),
          MySizeBox(height: 50),
          Center(
            child: MyButton(
              title: "提现到银行卡",
              onTap: () {
                onTap();
              },
            ),
          ),
        ],
      ),
    );
  }

//展示提示
  Widget buildHintWidget() {
    return Offstage(
      offstage: showHint,
      child: Container(
        padding: EdgeInsets.all(10.px),
        margin: EdgeInsets.only(bottom: 35.px),
        width: double.infinity,
        decoration: BoxDecoration(
          color: MyColors.formHex("#FF080822"),
        ),
        child: Row(
          children: [
            Image.asset(
              MyAssetsImages.setAssetsImagesPath("叉"),
              width: SizeFill.setRx(10),
              height: SizeFill.setRx(10),
            ),
            MySizeBox(width: 5),
            Expanded(
              child: Text(
                "为保证您的账号安全请绑定手机号，并尽快设置密码",
                style: TextStyle(
                  color: MyColors.formHex("#222222FF"),
                  fontSize: MyFont.font10,
                  fontWeight: MyFontWeight.light,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
